﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Details;
using KinonekoSoftware.CodariaDB.Documents.Keywords;
using KinonekoSoftware.CodariaDB.Documents.Templates;


namespace KinonekoSoftware.Studio.Pages.Theories
{
    public sealed partial class WorldViewConfigViewModel : RepositoryGalleryItem
    {

        private readonly IRepository                             _repository;
        private readonly RepositoryRuntime                       _runtime;
        private readonly Part_RepositoryBasic                    _Part_Basic;
        private readonly Part_RepositoryDetail                   _Part_Detail;
        private readonly Part_RepositoryStory                    _Part_Story;
        private readonly EnumValuesFilteringFactory<CatalogList> _catalogLists;
        private readonly KeywordCatalogRoot                      _keywordCatalogRoot;
        private          CatalogRoot                             _appCatalogRoot;

        private readonly ImageSystem    _engine_image;
        private readonly KeywordEngine  _engine_keyword;
        private readonly TemplateEngine _engine_template;


        public WorldViewConfigViewModel()
        {
            Platform.GetEngine<ImageSystem, KeywordEngine, TemplateEngine>(
                                                                           out _runtime,
                                                                           out _engine_image,
                                                                           out _engine_keyword,
                                                                           out _engine_template);

            //
            //d
            _repository           = _runtime.Repository;
            _catalogLists         = new EnumValuesFilteringFactory<CatalogList>(x => ((int)x) > 300);
            _filteredInstanceType = new EnumValuesFilteringFactory<InstanceType>(a => a       != InstanceType.Custom);
            _keywordCatalogRoot   = _repository.GetOrCreate(KeywordCatalogRoot.Create);

            // 初始化当前的标题
            TreeCatalogs                = new ObservableCollection<TreeCatalog>();
            KeywordCatalogs             = new ObservableCollection<KeywordCatalog>();
            KeywordChildren             = new ObservableCollection<KeywordCatalog>();
            Keywords                    = new ObservableCollection<string>();
            InstanceTemplates           = new ObservableCollection<InstanceTemplate>();
            CustomTypeInstanceTemplates = new ObservableCollection<InstanceTemplate>();
            Title                       = LanguageManager.GetText(CatalogRoot.Title_WorldView_Config);


            SaveCommand = CreateCommand(Save);

            

            NewKeywordCatalogCommand       = CreateCommand<KeywordCatalog>(DoNewKeywordCatalogCommand);
            EditKeywordCatalogCommand      = CreateCommand<KeywordCatalog>(DoEditKeywordCatalogCommand);
            MoveKeywordCatalogCommand      = CreateCommand<KeywordCatalog>(DoMoveKeywordCatalogCommand);
            ShiftUpKeywordCatalogCommand   = CreateCommand<KeywordCatalog>(DoShiftUpKeywordCatalogCommand);
            ShiftDownKeywordCatalogCommand = CreateCommand<KeywordCatalog>(DoShiftDownKeywordCatalogCommand);
            RemoveKeywordCatalogCommand    = CreateCommand<KeywordCatalog>(DoRemoveKeywordCatalogCommand);



            NewTreeCatalogCommand       = CreateCommand<TreeCatalog>(DoNewTreeCatalogCommand);
            EditTreeCatalogCommand      = CreateCommand<TreeCatalog>(DoEditTreeCatalogCommand);
            MoveTreeCatalogCommand      = CreateCommand<TreeCatalog>(DoMoveTreeCatalogCommand);
            ShiftUpTreeCatalogCommand   = CreateCommand<TreeCatalog>(DoShiftUpTreeCatalogCommand);
            ShiftDownTreeCatalogCommand = CreateCommand<TreeCatalog>(DoShiftDownTreeCatalogCommand);
            RemoveTreeCatalogCommand    = CreateCommand<TreeCatalog>(DoRemoveTreeCatalogCommand);
            ApplyCatalogChangedCommand  = CreateCommand(DoApplyCatalogChangedCommand);


            AddInstanceTemplateCommand        = CreateCommand(DoAddInstanceTemplateCommand);
            EditInstanceTemplateCommand       = CreateCommand<InstanceTemplate>(DoEditInstanceTemplateCommand);
            ReGenerateInstanceTemplateCommand = CreateCommand(DoReGenerateInstanceTemplateCommand);
            ExportInstanceTemplateCommand     = CreateCommand<InstanceTemplate>(DoExportInstanceTemplateCommand);
            ImportInstanceTemplateCommand     = CreateCommand<InstanceTemplate>(DoImportInstanceTemplateCommand);
            RemoveInstanceTemplateCommand     = CreateCommand<InstanceTemplate>(DoRemoveInstanceTemplateCommand);

            //
            // Initialized
            Initialized();
            InitializeKeywords();
            InitializeInstanceTemplates();
        }


        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------

        private void Initialized()
        {
        }

        private async void Save()
        {
            //
            //
            SaveTreeCatalogs();

            SaveKeywordCatalogs();

            // await ((Ioc.Get<AppViewModel>()).Snapshot(_repository));
            //await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }



        protected override void StartAfter()
        {
            Initialize();
            base.StartAfter();
        }

        protected override void ResumeBefore()
        {
            Initialize();
            base.ResumeBefore();
        }

        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------


        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------
        public ICommandEX SaveCommand { get; }
    }
}